<?php
/**
 * Created by PhpStorm.
 * User: dev
 * Date: 2018/7/26
 * Time: 13:54
 */
namespace app\home\controller;

use \app\common\controller\Frontend;
use think\Db;

class Blog extends Frontend
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = '*';
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = '*';

    protected $res = [];

    public function _initialize()
    {
        parent::_initialize();
    }

    /*
     * CTS首頁
     */
    public function blog(){
        $flag = isMobile();
        $param = $this->request->param();
        if(!empty($_GET)){
            $param['lang'] = $_GET['lang'];
        }
        $where = [];
        if(isset($param['keyword'])){
            $keyword = isset($param['keyword'])? $param['keyword'] : '';
        }

        $lang = 1;
        if(isset($param['lang']) && $param['lang'] == 'zh-cn'){
            $lang = 0;
        }

        if($lang == 1){
            //查询英文分类
            $en_where['pid'] = 9;
            $cate_rows = Db::name('article_category')->where($en_where)->select();
        }else{
            //查询中文分类
            $cn_where['pid'] = 8;
            $cate_rows = Db::name('article_category')->where($cn_where)->select();
        }
        //分类id
        $cate_ids = array_column($cate_rows, 'id');
        $where['articlecategory_id'] = ['in',$cate_ids];

        if(isset($param['keyword'])){
            $where['title'] = ['like','%'.$keyword.'%'];
        }else{
            if(isset($param['cate_id'])){
                $where['articlecategory_id'] = ['in',$param['cate_id']];
            }
        }
        if(isset($param['year'])){
            $start_time = strtotime($param['year'].'-0-0 0:0:0');
            $next_year = $param['year'] +1;
            $end_time = strtotime($next_year.'-0-0 0:0:0');
            $where['published_time'] = ['between',[$start_time,$end_time]];
        }
        if(isset($param['year_month'])){
            $date_int = explode('-', $param['year_month']);
            $start_time = strtotime($param['year_month'].'-0 0:0:0');
            $next_month = (int)$date_int[1]+1;
            $end_month = $date_int[0].'-'.$next_month;
            $end_time = strtotime($end_month.'-0 0:0:0');
            $where['published_time'] = ['between',[$start_time,$end_time]];
        }
        $where['publishstatus'] = 1;
        $field="id,articlecategory_id,user_id,clickrate,title,commentrate,goodrate,key_word,source,smallimage,publishstatus,published_time";
        $list = Db::name('article_manage')->field($field)->where($where)->order('published_time desc')->paginate(10);
        $this->assign('list',$list);
        $list->appends($where);
        $page = $list->render();
        $this->assign('page',$page);
        //归档
        $filing_where['publishstatus'] = 1;
        $filing_where['articlecategory_id'] = ['in',$cate_ids];
        $filing_rows = Db::name('article_manage')->where($filing_where)->select();
        $yearpige = [];
        $monthpige = [];
        foreach ($filing_rows as $key => &$val) {
            $year =  date('Y',$val['published_time']);
            $month =  date('m',$val['published_time']);
            if(!isset($yearpige[$year])){
                $yearpige[$year] = 1;
            }else{
                $yearpige[$year] = $yearpige[$year] + 1;
            }
            if(!isset($monthpige[$year][$month])){
                $monthpige[$year][$month] = 1;
            }else{
                $monthpige[$year][$month] +=  1;
            }
        }
        $this->assign('lang',isset($param['lang']) ? $param['lang'] : 'en');
        $this->assign('yearpige',$yearpige);
        $this->assign('monthpige',$monthpige);
        $this->assign('cate_rows',array_reverse($cate_rows));
        if( $flag ) {
            return $this->view->fetch('blog/blog_mobile');
        } else {
            return $this->view->fetch();
        }
    }

    /**
     * 詳情
     * @return string
     * @throws \think\Exception
     */
    public function detail()
    {
        $data = input();
        $id = $data['id'];
        $idpre = $id -1;
        $idbe = $id +1;
        $temp = '没有了';
        $param = $this->request->param();
        if(!empty($_GET)){
            $param['lang'] = $_GET['lang'];
        }
        if(!isset($param['id'])){
             $this->error("参数错误");
        }
        $lang = isset($param['lang']) ? $param['lang'] : 'en';
        //查询分类
        if($lang == 'en'){
            $en_where['pid'] = 9;
            $cate_rows = Db::name('article_category')->where($en_where)->select();
        }elseif($lang == 'zh-cn'){
            $cn_where['pid'] = 8;
            $cate_rows = Db::name('article_category')->where($cn_where)->select();
        }
        $this->assign('cate_rows',array_reverse($cate_rows));
        //归档
        $cate_ids = array_column($cate_rows, 'id');
        $filing_where['publishstatus'] = 1;
        $filing_where['articlecategory_id'] = ['in',$cate_ids];
        $filing_rows = Db::name('article_manage')->where($filing_where)->select();
        $yearpige = [];
        $monthpige = [];
        foreach ($filing_rows as $key => &$val) {
            $year =  date('Y',$val['published_time']);
            $month =  date('m',$val['published_time']);
            if(!isset($yearpige[$year])){
                $yearpige[$year] = 1;
            }else{
                $yearpige[$year] = $yearpige[$year] + 1;
            }
            if(!isset($monthpige[$year][$month])){
                $monthpige[$year][$month] = 1;
            }else{
                $monthpige[$year][$month] +=  1;
            }
        }
        $this->assign('lang',isset($param['lang']) ? $param['lang'] : 'en');
        $this->assign('yearpige',$yearpige);
        $this->assign('monthpige',$monthpige);

        if($id){
            $article_manage = Db::name('article_manage')->where('id',$data['id'])->find();
            $article_pre = Db::name('article_manage')->field('id,title')->where('id',"$idpre")->find();
            $article_be = Db::name('article_manage')->field('id,title')->where('id',"$idbe")->find();
            $user = Db::name('admin')->field('username')->where('id',$article_manage['user_id'])->find();
            $article_manage['article_pre'] =$article_pre?$article_pre['title']:$temp;
            $article_manage['article_pre_id'] =$article_pre?$article_pre['id']:$id;
            $article_manage['article_be'] =$article_be?$article_be['title']:$temp;
            $article_manage['article_be_id'] =$article_be?$article_be['id']:$id;
            $this->assign('article',$article_manage);
            $this->assign('user_config',$user);
            Db::name('article_manage')->where(['id' => $id])->setInc('clickrate');
        }else{
            $res = array(
                'status' => false,
                'msg'    => '该文章已不存在'
            );
            return json($res);
        }
        $flag = isMobile();
        if( $flag ) {
            return $this->view->fetch('blog/detail_mobile');
        } else {
            return $this->view->fetch();
        }
    }

    //文章时间归档
    public function pigeonhole($lang = 'zh-cn'){
        if( $lang == 'zh-cn' ) {
            $cate = Db::name('article_category')->where(['pid'=>8])->select();
        }elseif ( $lang == 'en' ){
            $cate = Db::name('article_category')->where(['pid'=>9])->select();
        }

        if(!empty($cate)){
            $cate_ids = array_column($cate, 'id');
        }else{
            $cate_ids = [];
        }
        //$where['p.post_type'] = 1;
        $where['b.id'] =  ['in',$cate_ids];
        //$where['a.delete_time'] = '';
        $post = Db::name('article_manage')->field('a.id,a.published_time')
                ->alias('a')
                ->join('article_category b','b.id=a.articlecategory_id','left')
                ->where($where)
                ->order('a.published_time desc')
                ->select();
        $yearpige = [];
        $monthpige = [];
        foreach ($post as $key => &$val) {
            $year =  date('Y',$val['published_time']);
            $month =  date('m',$val['published_time']);
            if(!isset($yearpige[$year])){
                $yearpige[$year] = 1;
            }else{
                $yearpige[$year] = $yearpige[$year] + 1;
            }
            if(!isset($monthpige[$year][$month])){
                $monthpige[$year][$month] = 1;
            }else{
                $monthpige[$year][$month] +=  1;
            }
        }
        return ['year'=>$yearpige,'month'=>$monthpige];
    }
}